<?php
/**
 *  
 *
  */
namespace Org\Util;
class DLJS {
		//工资结算
	public function dl_salary(){
			//查询代理
		$daili=M('member')->field('member_id')->where('is_agent=1')->select();
foreach($daili as $k=>$v){

	  //该代理下的会员
	  $member_list=M('Member')->where('parent_id='.$v['member_id'])->select();
	    $this->jiesuan_salary($member_list);

}

		
	$data=array('info'=>'结算成功','status'=>'1');	
		 		 
		$this->ajaxReturn($data,'json');
		exit(0);

	}
	//分红结算
	public function dl_fenhong(){
			//查询代理
		$daili=M('member')->field('member_id')->where('is_agent=1')->select();
foreach($daili as $k=>$v){

	  //该代理下的会员
	  $member_list=M('Member')->where('parent_id='.$v['member_id'])->select();
	    $this->jiesuan_fenhong($member_list);

}

		
	$data=array('info'=>'结算成功','status'=>'1');	
		 		 
		$this->ajaxReturn($data,'json');
		exit(0);

	}
	 //佣金结算
	public function dl_a(){
  		$condition['dl_status']=0;
  		$condition['status']=1;
		//$condition['pan']='A';
 		$order=M('lottery_order')->where($condition)->select();  //所有已结算,未结佣金订单
 		 if(empty($order)){
			$data=array('info'=>'没有要结算的订单','status'=>'-1');	
		 		 
		$this->ajaxReturn($data,'json');
		exit(0);
		}
		//查询下级
		$daili=M('member')->field('member_id,parent_id')->where('parent_id>0')->select();
foreach($daili as $k=>$v){
	 $this->js($v['member_id'],$v['parent_id']);
}
	
		$data=array('info'=>'结算成功','status'=>'1');	
		 		 
		$this->ajaxReturn($data,'json');
		exit(0);
		
 	}
	
	public function dl_c(){
  		$condition['dl_status']=0;
		$condition['pan']='C';
 		$order=M('lottery_order')->where($condition)->select();  //所有没有结算订单
 		if(empty($order)){
			$data=array('info'=>'没有要结算的数据','status'=>'-1');	
		 		 
		$this->ajaxReturn($data,'json');
		exit(0);
		}
		foreach($order as $ok=>$ov){
   		      $this->jiesuan($ov);
   		}
		$data=array('info'=>'结算成功','status'=>'1');	
		 		 
		$this->ajaxReturn($data,'json');
		exit(0);
		
 	}
	
	public function js($member_id,$parent_id){
		//查询凌晨2点前一天的所有订单,是否满足返利条件
		$amount=M('lottery_order')->where('dl_status!=1 and status=1 and m_id='.$member_id.' ')->sum('money');
		$win_amount=M('lottery_order')->where('dl_status!=1 and is_winning=1 and status=1 and m_id='.$member_id.' ')->sum('money');
		$lost_amount=M('lottery_order')->where('dl_status!=1 and is_winning=2 and status=1 and m_id='.$member_id.' ')->sum('money');
		$count=M('lottery_order')->where('dl_status!=1 and status=1 and m_id='.$member_id.' ')->count;
		//条件
		$dl_rebate=M('dl_rebate_setting')->where('amount>='.$amount)->order('amount asc')->find();
		$data['w_fanli']=$dl_rebate['percentage']*$win_amount;
		$data['l_fanli']=$dl_rebate['lost_percentage']*$lost_amount;
		$data['money']=$amount;
		$data['add_time']=time();
		$data['m_id']=$parent_id;
		$data['x_id']=$member_id;
		$data['number']=$count;
		$data['agent_name']=M('member')->where('member_id='.$parent_id)->getField('member_name');
		$data['f_money']=sprintf("%.3f",$data['w_fanli']+$data['l_fanli']);
		$data['remark']='下级会员:'.$member_id.'消费'.$dl_rebate['amount'].'元，返佣金'.$data['f_money'].'元';
		$result = M('dl_fanli')->add($data);
		//更新已发送佣金订单
		$order=M('lottery_order')->field('dl_status,order_id')->where('dl_status!=1 and status=1 and m_id='.$member_id.' ')->select();
		foreach($order as $k=>$v){
			$dd['dl_status']=1;
			M('lottery_order')->where('order_id='.$v['order_id'])->save($dd);
		}
		//代理增加佣金
		 $this->zhongjiang($data); 
	}
//工资结算
 	public function jiesuan_salary($info=array()){
 		$amount=0;
 		if(!empty($info)){


 		foreach($info as $k=>$v){
 		$condition['member_id']=$v['member_id'];
 		$condition['dl_status']=0;
  		$condition['status']=1;
		
 		$money=M('lottery_order')->where($condition)->sum('money');  
 		$amount+=$money;
 		$parent_id=$v['parent_id'];

 		//更新已发工资订单
		$order=M('lottery_order')->field('dl_status,order_id')->where('dl_salary!=1 and status=1 and m_id='.$v['member_id'].' ')->select();
		foreach($order as $k=>$v){
			$dd['dl_salary']=1;
			M('lottery_order')->where('order_id='.$v['order_id'])->save($dd);
		}


 		}
 		
 		$amount_new=$amount/10000;
 		$setting=M('salary_setting')->where('sales>='.$amount_new)->order('sales asc')->find();
 		$sales_money=$amount*$setting['percentage']/100;
 		$data['m_id']=$parent_id;
 		$data['add_time']=time();
 		$data['salary']=$sales_money;
 		$data['money']=$amount;
 		$data['agent_name']=M('member')->where('member_id='.$parent_id)->getField('member_name');

 		$result = M('dl_salary')->add($data);


 		//发送给用户的信息
 		$log=array();
 		$log['m_id']=$parent_id;
 		$log['change_money']=$sales_money;
 		$log['change_type']=5;
 		$log['add_time']=time();
 		$log['remark']=date('Y-m-d').'日，工资结算';
 		M('account_log')->add($log);
  
 		M('member')->where('member_id='.$parent_id)->setInc('member_money',$sales_money); // 用户的余额增加
 	}

 	}

//分红结算
 	public function jiesuan_fenhong($info=array()){
 		$amount=0;
 		$amount_all=0;
 		$lost_money=0;
 		if(!empty($info)){
 		foreach($info as $k=>$v){
 		$condition['member_id']=$v['member_id'];
 		$condition['dl_status']=0;
  		$condition['status']=1;
		
 		$money=M('lottery_order')->where($condition)->sum('win_money');  
 		$amount+=$money;
 		$lost_money=M('lottery_order')->where($condition)->sum('lost_money');  
 		$lost_money+=$lost_money;
 		//下注总额
 		$money2=M('lottery_order')->where($condition)->sum('money');  
 		$amount_all+=$money2;


 		$parent_id=$v['parent_id'];

 		//更新已发分红订单
		$order=M('lottery_order')->field('dl_status,order_id')->where('dl_fenhong!=1 and status=1 and m_id='.$v['member_id'].' ')->select();
		foreach($order as $k=>$v){
			$dd['dl_fenhong']=1;
			M('lottery_order')->where('order_id='.$v['order_id'])->save($dd);
		}


 		}
$amount=$lost_money-$amount;
 		
 		if($amount>0){
 			//有盈利

 		$amount_new=$amount_all/10000;
 		$setting=M('fenhong_setting')->where('sales>='.$amount_new)->order('sales asc')->find();
 		$sales_money=$amount*$setting['percentage']/100;
 		$data['m_id']=$parent_id;
 		$data['add_time']=time();
 		$data['fenhong']=$sales_money;
 		$data['money']=$amount_all;
 		$data['money_win']=$amount;
 		$data['agent_name']=M('member')->where('member_id='.$parent_id)->getField('member_name');

 		$result = M('dl_fenhong')->add($data);

 		//发送给用户的信息
 		$log=array();
 		$log['m_id']=$parent_id;
 		$log['change_money']=$sales_money;
 		$log['change_type']=5;
 		$log['add_time']=time();
 		$log['remark']=date('Y-m-d').'日，分红结算';
 		M('account_log')->add($log);

 		M('member')->where('member_id='.$parent_id)->setInc('member_money',$sales_money); // 用户的余额增加
 			}
 		}
 	}


	public function jiesuan($info=array())
	{
	 
	   //反水比例
	   $p_rebate=M('dl_rebate_setting')->find();
	   $k=1;	   
	   do{
 	    if($k==1){	   
	       $m_info[$k]=D('Member')->getOneMember($info['m_id']);  //下注会员信息
		  // $p_info[$k]=D('Member')->getOneMember($m_info[$k]['parent_id']);  //上一级代理会员信息
		   $p_rebate[$k]=unserialize($m_info[$k][$name]);
		   $insert_array=array();
		   $insert_array['m_id']=$m_info[$k]['member_id'];
		   $insert_array['x_id']=$m_info[$k]['member_id'];
		   $insert_array['c_id']=$info['c_id'];
		   $insert_array['c_name']=$info['c_name'];
		   $insert_array['add_time']=time();
		   $insert_array['money']=$info['money'];
		   $insert_array['pan']=$info['pan'];
		   $insert_array['p_rebate']=$p_rebate[$k][$info['c_id']];
		   $insert_array['f_money']=(float)$p_rebate[$k][$info['c_id']]*$info['money']/100;
		   $insert_array['remark']='代理:'.$m_info[$k]['member_name'].'下注反水';
 		   //print_r($p_rebate[$k]);exit;
		   if($insert_array['p_rebate']>0){  //反水级差大于0才可结算
		   //$result = M('dl_fanli')->add($insert_array);
		  // $this->zhongjiang($insert_array); 
		   }
	   }else{
 	     $m_info[$k]=D('Member')->getOneMember($m_info[$k-1]['member_id']);  //下注会员信息	
		 if($m_info[$k]['parent_id']>0){   //直推代理
			 $p_info[$k]=D('Member')->getOneMember($m_info[$k]['parent_id']);  //上一级代理会员信息
			 $p_rebate[$k]=unserialize($p_info[$k][$name]);
			 $insert_array=array();
			 $insert_array['m_id']=$p_info[$k]['member_id'];
			 $insert_array['x_id']=$m_info[$k]['member_id'];
			 $insert_array['c_id']=$info['c_id'];
			 $insert_array['c_name']=$info['c_name'];
			 $insert_array['add_time']=time();
			 $insert_array['money']=$info['money'];
			 $insert_array['pan']=$info['pan'];
			 $rank_id=$m_info['member_rank'];
			 $fanli=M('member_rank')->where('rank_id='.$rank_id)->getField('fanli');
			 if($fanli!=''){
			 	$insert_array['p_rebate']=$fanli;
			 }else{
$insert_array['p_rebate']=$p_rebate['percentage'];
			 }
			 
			 $insert_array['f_money']=(float)$insert_array['p_rebate']*$info['money']/100; 
			// $insert_array['remark']='推荐'.$k.'级代理:'.$m_info[$k]['member_name'].'下注反水';
			$insert_array['remark']='推荐会员:'.$m_info[$k]['member_name'].'下注反水';
			 $result = M('dl_fanli')->add($insert_array);
			 $this->zhongjiang($insert_array); 
		 }   
	   }
  	   $k++;
 	   }while($k<3);
	   $map['order_id']=$info['order_id'];
	   $data['dl_status']=1;
	   $data['dl_js_time']=time();
	   //print_r($data);exit;
 	   M('lottery_order')->where($map)->save($data);
	}
 
 public function zhongjiang($info){
 	$money=$info['f_money'];
  	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
	
	}
}